package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.google.gson.Gson;

import bean.Menu;
import util.DBUtil;

/**
 * Servlet implementation class getDateServlet
 */
@WebServlet("/servlet/getData")
public class GetDataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String menu_id = request.getParameter("menu_id");
		String sql = "select * from menu where grade>1 and FIND_IN_SET(menu_Id,fn_getAddress_ChildList_test("+menu_id+"))";
		try {
			Connection conn = DBUtil.getConn();

			QueryRunner queryRunner = new QueryRunner();

			List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();

			List<Menu> menuList = queryRunner.query(conn, sql, new BeanListHandler<>(Menu.class));
			Map<String, Map<String, Object>> id_nodeMap = new HashMap<String, Map<String, Object>>();
			Map<String, Object> nodeMap = null;
			for (Menu menu : menuList) {
				nodeMap = new HashMap<String, Object>();
				nodeMap.put("id", menu.getMenu_id());
				nodeMap.put("text", menu.getMenu_name());
				nodeMap.put("href1", menu.getHref());
				id_nodeMap.put(String.valueOf(menu.getMenu_id()), nodeMap);

				if (menu.getParent_id() == Integer.valueOf(menu_id)) {
					treeList.add(nodeMap);
				} else {

					Map<String, Object> parenMap = id_nodeMap.get(String.valueOf(menu.getParent_id()));

					if (parenMap != null) {
						List<Map<String, Object>> children = null;

						if (parenMap.get("children") == null) {
							// 说明该父节点当前还没有一个子节点
							children = new ArrayList<Map<String, Object>>();
						} else {
							children = (List<Map<String, Object>>) parenMap.get("children");
						}
						children.add(nodeMap);
						parenMap.put("children", children);
					}

				}

			}

			Gson gson = new Gson();

			String json_res = gson.toJson(treeList);

			out.print(json_res);
			out.flush();
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

	}
}